const db = require('../db/dbs');

module.exports = {
    // 获取特定话题的图片和其对应的回答
    async getTopicData(TopicID) {
        const selectQuery = `
            SELECT t.TopicID, t.TopicPicture, a.AnswerID, a.Answer, a.Judge
            FROM topic AS t
            LEFT JOIN answer AS a ON t.TopicID = a.TopicID
            WHERE t.TopicID = ?;
        `;
        try {
            const rows = await db.query(selectQuery, [TopicID]);
            const topicData = {
                TopicPicture: rows[0].TopicPicture,
                Answer: rows.map(row => ({
                    AnswerID: row.AnswerID,
                    Answer: row.Answer,
                    Judge: row.Judge
                }))
            };
            return topicData;
        } catch (error) {
            throw new Error(`无法获取话题ID为 ${TopicID} 的话题和回答数据: ${error.message}`);
        }
    }
};